#@layout("角色授权")
#define css()
	<link rel="stylesheet" href="#(ctxAdSt)/lib/zTree_v3/css/zTreeStyle/zTreeStyle.css" type="text/css">
    <style>
        .ztree{
            margin-left: 12px;
            margin-bottom: 70px;
        }
        .timo-compile .timo-finally{
            position: fixed;
            bottom: 0;
            left: 0;
            right: 0;
            padding-bottom: 14px;
            margin-bottom: 0;
            background-color: #ffffff;
        }
    </style>
#end
#define main()
<div class="layui-form timo-compile">
    <ul id="authTree" class="ztree" data-url="#(ctxAd)/role/authList?id=#(id)" ></ul>
    <div class="layui-form-item timo-finally">
        <button id="submit" class="layui-btn" data-url="#(ctxAd)/role/auth" data-id="#(id)" ><i class="fa fa-check-circle"></i> 保存</button>
        <button class="layui-btn btn-secondary close-popup"><i class="fa fa-times-circle"></i> 关闭</button>
    </div>
</div>
#end
#define js()
	<script type="text/javascript" src="#(ctxAdSt)/js/plugins/jquery-3.3.1.min.js"></script>
	<script type="text/javascript" src="#(ctxAdSt)/lib/zTree_v3/js/jquery.ztree.core.min.js"></script>
	<script type="text/javascript" src="#(ctxAdSt)/lib/zTree_v3/js/jquery.ztree.excheck.min.js"></script>
	<SCRIPT type="text/javascript">
	    $(function(){
	        var setting = {
	            check: {
	                enable: true,
	                chkboxType: { "Y" : "ps", "N" : "ps" }
	            },
	            data: {
	                simpleData: {
	                    enable: true
	                }
	            }
	        };
	        $.get($("#authTree").data("url"), function(result){
	            var keyPid = [];
	            result.data.forEach(function(item){
	                keyPid[item.pid] = true;
	            });
	            var zNodes =[];
	            result.data.forEach(function (item) {
	                var menu = {
	                    id: item.id,
	                    pId: item.pid,
	                    name: item.title
	                };
	                if(item.pid === 0){
	                    menu.open = true;
	                }
	                if(item.url.indexOf("/index") !== -1 && keyPid[item.id]){
	                    var index = {
	                        id: '-'+item.id,
	                        pId: item.id,
	                        name: "列表"
	                    };
	                    if(item.description === "auth:true"){
	                        index.checked = true;
	                    }
	                    zNodes.push(index);
	                }
	                if(item.description === "auth:true"){
	                    menu.checked = true;
	                }
	                zNodes.push(menu);
	            });
	            $.fn.zTree.init($("#authTree"), setting, zNodes);
	        });
	
	        $("#submit").click(function () {
	            var zTreeObj = $.fn.zTree.getZTreeObj("authTree");
	            var authList = zTreeObj.getCheckedNodes(true);
	            var authIds = [];
	            authIds.push("id="+$(this).data("id"));
	            authList.forEach(function(item){
	                if(item.id.substr(0,1) != ('-')){
	                    authIds.push("authId="+item.id);
	                }
	            });
	            $.post($(this).data("url"), authIds.join("&"), function(result){
	                if (result.data == null) {
	                    result.data = 'submit[refresh]';
	                }
	                $.fn.Messager(result);
	            });
	        });
	    });
	</SCRIPT>
#end